
function updateStatus() {
    $.ajax({
            url:getStatusUrl,
            success:function (data, textStatus, jqXHR) {
                if (data != null && data.isError !== undefined) {
                    var isError = false;
                    isError = data.isError;
                    setTimeout('updateStatus()', 1000);
                    updateProgressBar(data);
                }
                else {
                    setTimeout('updateStatus()', 1000);
                }
            }
        }
    );
}
function getCurrentCategory(currentSitePath) {
    var result;
    if (currentSitePath.childPathElement != null && currentSitePath.childPathElement.childPathElement != null) {
        result = getCurrentCategory(currentSitePath.childPathElement);
    } else {
        return currentSitePath;
    }
    return result;
}
function getSitePath(currentSitePath, list) {

    if (currentSitePath.childPathElement != null) {
        var link = $("<a />")
            .attr("href", currentSitePath.url)
            .text(currentSitePath.title == null ? "Каталог" : currentSitePath.title);
        if (list == null) {
            list = $("<ul />");
        }
        var listItem = $("<li/>").append(link);
        list.append(listItem);
        getSitePath(currentSitePath.childPathElement, list);
    } else {
        if (!currentSitePath.isProduct)
            alert('hello');
        $("#productTitle").attr("href", currentSitePath.url).text(currentSitePath.title);
        $("#sitePath").empty().append(list);
    }
}
function updateProgressBar(data) {

    var overallPercent = (data.categoryIndex / data.categoriesCount) * 100;
    $("#categoryIndex").text(data.categoryIndex);
    $("#totalCategoriesCount").text(data.categoriesCount);
    var overallProductPercent = (data.productIndex / data.productsCount) * 100;
    var categoryPath = getCurrentCategory(data.currentSitePath);
    var categoryPercent;
    if (categoryPath.containsProducts) {
        categoryPercent = (categoryPath.childIndex / categoryPath.itemsCount) * 100;
        $('#categoryProgress').animate({width:categoryPercent + "%"}, 300, function () {
            getSitePath(data.currentSitePath);
            var categoryPercentText = categoryPercent.toFixed(0) + '%';
            if (categoryPercentText != 'NaN%')
                $('#categoryProgressText').html(categoryPercentText);
            $("#categoryProductIndex").text(categoryPath.childIndex);
            $("#totalCategoryProductsCount").text(categoryPath.itemsCount);
        });

    }
    $('#overallProgress').animate({width:overallPercent + "%"}, 200, function () {
    });
    $('#overallProgressText').html(overallPercent.toFixed(0) + '%');

    $("#productIndex").text(data.productIndex);
    $("#totalProductsCount").text(data.productsCount);


}
$(document).ready(function () {
    updateStatus()
});
